package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.MallWechatConfig;
import com.rockcent.mall.lib.domain.SysMessage;
import com.rockcent.mall.lib.domain.SysMessageUser;
import com.rockcent.mall.lib.domain.enumclass.SysMessageStatus;
import com.rockcent.mall.lib.domain.enumclass.SysMessageType;
import com.rockcent.mall.lib.dto.SysMessageDto;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by zhangjh on 2017/5/27.
 */
@Repository
public class SysMessageRepositoryImpl implements SysMessageRepositoryCustom {


    private static final Logger log = LoggerFactory.getLogger(SysMessageRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;


    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<SysMessageDto> querySysMessage(SysMessageDto sysMessage, PageResult pageResult) {
        SQLPageDoing<SysMessageDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM sys_message s ,sys_message_user p where p.msg_id= s.id and p.is_delete='NO' and p.user_id =:userId");
            fields.append(" s.content,s.type,s.backend_url,s.frontend_url,p.id ,p.date_created,p.status");
            String status = sysMessage.getStatus();
            String type = sysMessage.getType();
            if (StringUtils.isNotBlank(status) && !SysMessageStatus.ALL.toString().equals(status)) {
                sql.append("  and p.status = :status");
                params.put("status", status);
            }
            if (StringUtils.isNotBlank(type) && !SysMessageType.ALL.toString().equals(type)) {
                sql.append(" and s.type = :type");
                params.put("type", type);
            }
            sql.append(" and s.terminal_type = :terminalType");
            params.put("terminalType", sysMessage.getTerminalType());

            sql.append(" and p.destination_terminal_id = :destinationTerminalId");
            params.put("destinationTerminalId", sysMessage.getDestinationTerminalId());

            sql.append(" order by  p.status desc,p.date_created desc");
            params.put("userId", sysMessage.getUserId());

        };
        return doing.go(pageResult, SysMessageDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public SysMessageDto findOneByIdAndIsDeleteAndUserId(Long msgId, Whether no, Long userId) {
        SQLObjectDoing<SysMessageDto> result = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s from sys_message s ,sys_message_user p where p.msg_id= s.id and p.is_delete='NO' and p.user_id =:userId and p.id = :msgId limit 1");
            params.put("userId", userId);
            params.put("msgId", msgId);
            fields.append("s.content,s.type,s.backend_url,s.frontend_url,p.id,p.date_created,p.status");
        };
        return result.go(SysMessageDto.class, jdbcTemplate, log, objectMapper);


    }
}
